package cn.minimelon.api.controller.system;


import cn.hutool.core.util.ObjUtil;
import cn.minimelon.api.controller.common.BaseController;
import cn.minimelon.api.domain.common.AjaxResult;
import cn.minimelon.api.domain.common.TableDataInfo;
import cn.minimelon.api.domain.system.SysDictData;
import cn.minimelon.api.service.system.ISysDictDataService;
import cn.minimelon.api.service.system.ISysDictTypeService;
import org.noear.solon.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * 数据字典信息
 *
 * @author ruoyi
 */
@Controller
@Mapping("/system/dict/data")
public class SysDictDataController extends BaseController {
    @Inject
    ISysDictDataService dictDataService;

    @Inject
    ISysDictTypeService dictTypeService;

    @Get
    @Mapping("/list")
    public TableDataInfo list(SysDictData dictData) {
        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }

    /**
     * 查询字典数据详细
     */
    @Get
    @Mapping(value = "/{dictCode}")
    public AjaxResult getInfo(Long dictCode) {
        return AjaxResult.success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @Get
    @Mapping(value = "/type/{dictType}")
    public AjaxResult dictType(String dictType) {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (ObjUtil.isNull(data)) {
            data = new ArrayList<>();
        }
        return AjaxResult.success(data);
    }

    /**
     * 新增字典类型
     */
    @Post
    @Mapping
    public AjaxResult add(SysDictData dict) {
        dict.setCreateBy(getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @Put
    @Mapping
    public AjaxResult edit(SysDictData dict) {
        dict.setUpdateBy(getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @Delete
    @Mapping("/{dictCodes}")
    public AjaxResult remove(Long[] dictCodes) {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
